package main

import (
	"fmt"
	"gitee.com/lichenxin/pkg/logs"
	"github.com/pkg/errors"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {
	//read()
	start()
}

func start() {
	var lastTime string
	times := make(map[string]int64)
	err := logs.GZLogsRange("/media/levi/软件/logs/party", func(data []byte) error {
		l, err := logs.Parse(data)
		if err != nil {
			return errors.Wrap(err, "logs parse")
		}

		key := l.Method+"_"+l.Path
		if _, ok := times[key]; !ok {
			times[key] = 0
		}
		times[key]++
		if !strings.Contains(l.Time, lastTime) {
			for k, v := range times {
				if v > 50 {
					fmt.Println(fmt.Sprintf("%d\t%s", v, k))
				}
			}
			times = make(map[string]int64)
		}
		lastTime = l.Time
		return nil
	})
	if err != nil {
		panic(err)
	}
}

func read()  {
	data, err := ioutil.ReadFile("/tmp/api-max.txt")
	if err != nil {
		panic(err)
	}
	rows := strings.Split(string(data), "\n")
	res := make(map[string]int)
	for _, row := range rows {
		v := strings.Split(row, "\t")

		if len(v) != 2 {
			continue
		}

		count, err := strconv.Atoi(v[0])
		if err != nil {
			panic(err)
		}

		key := v[1]
		if _, ok := res[key]; !ok {
			res[key] = count
		} else {
			if res[key] < count {
				res[key] = count
			}
		}
	}

	for k, v := range res {
		fmt.Println(fmt.Sprintf("%d	%s", v, k))
	}
	os.Exit(1)
}
